package com.lihaozhe.config;

import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl;
import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository;

import javax.sql.DataSource;

/**
 * @author 李昊哲
 * @version 1.0.0
 * 2021/12/15 12:53
 */
@RequiredArgsConstructor
@Configuration
public class ApplicationConfig {
    private final DataSource dataSource;
    @Bean
    public PasswordEncoder newPasswordEncoder() {
        // 密码加密解密实现类
        return new BCryptPasswordEncoder();
    }
    @Bean
    public PersistentTokenRepository newPersistentTokenRepository(){
        JdbcTokenRepositoryImpl jdbcTokenRepository = new JdbcTokenRepositoryImpl();
        jdbcTokenRepository.setDataSource(dataSource);
        // 启动的时候自动创建表 第二次启动注释该行
        // jdbcTokenRepository.setCreateTableOnStartup(true);
        return jdbcTokenRepository;
    }
}
